//
//  MessagesViews.swift
//  Kosspot
//
//  Created by 李鑫 on 2025/6/19.
//

import SwiftUI

struct MessagesView: View {
    @State private var selectedDay = TimestampHelper.getDayName()
        let currentDate = Date()
        var daysInMonth: [Int] {
            let range = Calendar.current.range(of: .day, in: .month, for: currentDate)!
            return Array(range)
        }
    
    let sampleData: [DataPoint] = [
            DataPoint(value: 120, label: "00:00"),
            DataPoint(value: 180, label: "06:00"),
            DataPoint(value: 90, label: "12:00"),
            DataPoint(value: 210, label: "18:00"),
            DataPoint(value: 150, label: "24:00"),
        ]
    var body: some View {
        
            VStack {
                HStack{
                    Spacer()
                    Text("总运动")
                        .font(.system(size: 18))
                        .foregroundStyle(.white)
                        .padding(.top,58)
                    Spacer()
                }
                Spacer().frame(height: 25)
                HorizontalCalendarView(
                    days: daysInMonth,
                    selectedDay: $selectedDay,
                    currentDate: currentDate
                )
                .frame(height: 50)
                .background(.clear)
                
                ScrollView (.vertical) {
                    SportLineChartView(
                        data: sampleData,
                        lineColor: .blue,
                        lineWidth: 1,
                        showPoints: true,
                        pointSize: 2
                    )
                    .background(.clear)
                    .frame(height: 250)
                    .padding(.horizontal,24)
                    .padding(.top,-10)
                    
//                    Spacer().frame(height: 20)
                    VStack () {
                        HStack(spacing: 20) {
                            Text("运动数据分析")
                                .font(.system(size: 16))
                                .foregroundStyle(.white)
                            Spacer()
                        }
                        .padding(.horizontal,20)
                        .padding(.top,20)
                        HStack  {
                            Text("88").font(.system(size: 65)).foregroundColor(.white)
                           + Text("%").font(.system(size: 24)).foregroundColor(.white)
                            Spacer()
                            Image("6521750332421_.pic")
                                .resizable()
                                .frame(width: 156,height: 44)
                            
                        }.padding(.horizontal,20)
                        HStack {
                            Text("已完成").font(.system(size: 14)).foregroundColor(Color(hex: "#B2B2B2"))
                            Spacer()
                        }.padding(.horizontal,20)
                        
                        Spacer().frame(height: 20)
                        HStack (spacing: 20){
                            VStack (alignment: .leading,spacing: 12) {
                                Text("880").font(.system(size: 20)).foregroundColor(.white)
                                Text("总消耗（千卡）").font(.system(size: 12)).foregroundColor(Color(hex: "#B2B2B2"))
                            }
                            Spacer().frame(width: 50)
                            VStack (alignment: .leading,spacing: 12) {
                                Text("63").font(.system(size: 20)).foregroundColor(.white)
                                Text("总时长（分钟）").font(.system(size: 12)).foregroundColor(Color(hex: "#B2B2B2"))
                            }
                            Spacer()
                        }.padding(.horizontal,20)
                        
                        Spacer().frame(height: 18)
                        SportProgressView(progressValue: 0.88, sportName: "跑步", sportValue: "1.88公里",tintColor: Color(hex: "#68DD64"))
                        Spacer().frame(height: 18)
                        SportProgressView(progressValue: 0.60, sportName: "游泳", sportValue: "1.88千米",tintColor: Color(hex: "#2CFBE5"))
                        Spacer().frame(height: 18)
                        SportProgressView(progressValue: 0.60, sportName: "骑行", sportValue: "1.88公里",tintColor: Color(hex: "#55B4FF"))
                        Spacer()
                    }
                    .frame(height: 500)
                    .background(Color(hex: "#1E1E1E"))
                    .cornerRadius(16)
                    .padding(.horizontal,12)
                    Spacer()
                }
        }
        .background(Color(hex: "#0A0A0A"))
        .edgesIgnoringSafeArea(.all)
        .navigationBarHidden(true)
        .navigationBarTitleDisplayMode(.inline)
        .onChange(of: selectedDay) {newValue in
            print(newValue)
            
        }
    }
}



struct SportProgressView: View {
    @State  var progressValue: CGFloat
    @State  var sportName: String
    @State  var sportValue: String
    @State  var tintColor: Color

    var body: some View {
        VStack (alignment: .leading) {
            HStack {
                Text(sportName)
                    .font(.system(size: 16))
                Spacer()
            }
            Spacer().frame(height: 8)
            HStack {
                Text((sportValue))
                    .font(.system(size: 16))
                Spacer()
                Text(String(format: "%.0f%%", progressValue*100))
            }
            HStack {
                ProgressView(value: progressValue, total: 1.0)
                    .progressViewStyle(LinearProgressViewStyle(tint: tintColor))
                    .frame(height: 6)
            }
        }.padding(.horizontal,20)
            .foregroundStyle(.white)
    }
}










#Preview {
    MessagesView()
}
